package com.wsc.indexer;

import java.io.File;
import java.io.FileNotFoundException;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
import org.apache.http.HttpHost;

import com.wsc.Config;
import com.wsc.crawler.init.CrawlerConfig;

public class IndexerConfig implements Config{

	public XMLConfiguration config = null;
	
	private HttpHost indexserver = null;
	private String index_root_dir = null;
	private String index_2xx_dir = null;
	private String index_4xx_dir = null;
	private String index_3xx_dir = null;
	private String index_other_dir = null;
	private String WSDL_visited_index_dir = null;
	
	
	
	public String getIndex_other_dir() {
		
		index_other_dir =  (String) config
				.getProperty("/property/entry[@name='other_index_dir']");
		
		index_other_dir=getIndex_root_dir()+index_other_dir;
		return index_other_dir;
	}

	public void setIndex_other_dir(String index_other_dir) {
		this.index_other_dir = index_other_dir;
	}

	
	public HttpHost getIndexserver() {
		
		String host = (String) config
				.getProperty("/property/entry[@name='indexserver']");

		HierarchicalConfiguration node = config
				.configurationAt("/property/entry[@name='indexserver']");
		int port = Integer.parseInt(node.getString("@port"));

		indexserver = new HttpHost(host, port);
		
		return indexserver;
	}

	public void setIndexserver(HttpHost indexserver) {
		this.indexserver = indexserver;
	}

	public String getIndex_root_dir() {
		
		index_root_dir =  (String) config
				.getProperty("/property/entry[@name='index_root_dir']");
		return index_root_dir;
	}

	public void setIndex_root_dir(String index_root_dir) {
		this.index_root_dir = index_root_dir;
	}

	public String getIndex_2xx_dir() {
		
		index_2xx_dir =  (String) config
				.getProperty("/property/entry[@name='2xx_index_dir']");
		
		// return complete index path where urls are that returned 2XX status code are indexed, .
		
		index_2xx_dir = getIndex_root_dir()+index_2xx_dir;
		
		return index_2xx_dir;
	}

	
	public void setIndex_2xx_dir(String index_2xx_dir) {
		this.index_2xx_dir = index_2xx_dir;
	}

	public String getIndex_4xx_dir() {
		
		index_4xx_dir =  (String) config
				.getProperty("/property/entry[@name='4xx_index_dir']");
		
		// return complete index path where urls are that returned 2XX status code are indexed, .
		
		index_4xx_dir = getIndex_root_dir()+index_4xx_dir;

		return index_4xx_dir;
	}

	public void setIndex_4xx_dir(String index_4xx_dir) {
		this.index_4xx_dir = index_4xx_dir;
	}

	
	public String getIndex_3xx_dir() {
		

		index_3xx_dir =  (String) config
				.getProperty("/property/entry[@name='3xx_index_dir']");
		
		// return complete index path where urls are that returned 2XX status code are indexed, .
		
		index_3xx_dir = getIndex_root_dir()+index_3xx_dir;
		
		return index_3xx_dir;
	}

	public void setIndex_3xx_dir(String index_3xx_dir) {
		this.index_3xx_dir = index_3xx_dir;
	}

	public String getWSDL_visited_index_dir() {
		

		WSDL_visited_index_dir =  (String) config
				.getProperty("/property/entry[@name='WSDL_visited_index_dir']");
		
		// return complete index path where urls are that returned 2XX status code are indexed, .
		
		WSDL_visited_index_dir = getIndex_root_dir()+WSDL_visited_index_dir;
		return WSDL_visited_index_dir;
	}

	
	public void setWSDL_visited_index_dir(String wSDL_visited_index_dir) {
		WSDL_visited_index_dir = wSDL_visited_index_dir;
	}

	@Override
	public void loadXmlFile(File file) throws FileNotFoundException {
		
		File f;
		// check file existence
		if (file != null && file.exists()) {
			f = file;
		} else {
			// default file location
			f = new File("config/indexer-core.xml");
		}
		// System.out.println(f.toString());
		// create new xmlconfig obj
		try {
			config = new XMLConfiguration(f);

			// config.load();
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// set expression engine to exexute xpath queries on config.xml file

		config.setExpressionEngine(new XPathExpressionEngine());
	}

	// Load Configuration from Default file at Default location.
	
	@Override
	public void loadDefaultXmlFile() {
		File f = new File("config/indexer-core.xml");
		if (!f.exists()) {
			try {
				throw new FileNotFoundException();
			} catch (FileNotFoundException e) {
				System.out.println("crawler-core.xml file is not found in "
						+ f.getPath() + " Location.");
			}
		} else {
			try {
				config = new XMLConfiguration(f);
			} catch (ConfigurationException e) {
				System.out.println("Configuration exception in "
						+ CrawlerConfig.class.getCanonicalName());
			}
			config.setExpressionEngine(new XPathExpressionEngine());
		}
	}

	@Override
	public boolean isValidConfig() {
		// TODO Auto-generated method stub
		return false;
	}


}
